package cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.compare;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dml.CompareInstanceListResult;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import org.junit.Assert;

import java.util.ArrayList;
import java.util.List;

public class CompareUserInstanceListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setCurrentUserList(flowContext);
        synchronizedInstanceList(flowContext);
    }

    @Override
    public String name() {
        return "同步比较用户实例列表";
    }

    private void setCurrentUserList(FlowContext flowContext){
        List<User> currentUserList = new ArrayList<>();
        for(int i=0;i<=5;i++){
            currentUserList.add(getUserByIndex(i));
        }
        for(int i=20;i<=25;i++){
            currentUserList.add(getUserByIndex(i));
        }
        flowContext.putTemporaryData("currentUserList", currentUserList);
    }

    private void synchronizedInstanceList(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> currentUserList = (List<User>) flowContext.checkData("currentUserList");

        List<User> userList = dao.query(User.class).execute().getList();

        CompareInstanceListResult<User> compareInstanceListResult = dao.compareInstanceList(currentUserList, userList);
        //要插入的
        {
            List<String> usernameList = QueryInstanceList.newQuery(compareInstanceListResult.insertInstanceList)
                    .execute()
                    .getSingleFieldValueList("username");
            Assert.assertEquals(6, usernameList.size());
            for(int i=20;i<=25;i++){
                Assert.assertTrue(usernameList.contains("user"+i));
            }
        }
        //要更新的
        {
            List<String> usernameList = QueryInstanceList.newQuery(compareInstanceListResult.updateInstanceList)
                    .execute()
                    .getSingleFieldValueList("username");
            Assert.assertEquals(6, usernameList.size());
            for(int i=0;i<=5;i++){
                Assert.assertTrue(usernameList.contains("user"+i));
            }
        }
        //要删除的
        {
            List<String> usernameList = QueryInstanceList.newQuery(compareInstanceListResult.deleteInstanceList)
                    .execute()
                    .getSingleFieldValueList("username");
            Assert.assertEquals(5, usernameList.size());
            for(int i=6;i<=10;i++){
                Assert.assertTrue(usernameList.contains("user"+i));
            }
        }
    }

    private User getUserByIndex(int index){
        User user = new User();
        user.setUsername("user"+index);
        user.setPassword("123456");
        user.setEmail("user"+index+"@fake.com");
        user.setMobile("12345678911");
        return user;
    }
}
